<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GoodsWatchLog;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodsWatchLogController extends AdminController
{

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $user_id = \Request::get("user_id");
        $user = User::find($user_id);
        return $content
            ->title($user->nickname . "的浏览记录")
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new GoodsWatchLog(["goods"]), function (Grid $grid) {
            $grid->model()->where("user_id", \Request::get("user_id"));
            $grid->model()->orderBy("id", "desc");
            $grid->column('id')->sortable();
//            $grid->column('ip_addr');
            $grid->column('goods.name', "查看的产品");
//            $grid->column('user_id');
            $grid->column('created_at', "查看日期");
//            $grid->column('updated_at')->sortable();

            // 禁用生成按钮
            $grid->disableCreateButton();
            // 禁用
            $grid->disableActions();

            // 禁用删除按钮
            $grid->disableDeleteButton();
            // 禁用
            $grid->disableBatchActions();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsWatchLog(), function (Show $show) {
            $show->field('id');
            $show->field('ip_addr');
            $show->field('goods_id');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsWatchLog(), function (Form $form) {
            $form->display('id');
            $form->text('ip_addr');
            $form->text('goods_id');
            $form->text('user_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
